package cn.csc.module.shop.controller.admin.banner;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerCreateReqVO;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerPageReqVO;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerRespVO;
import cn.csc.module.shop.controller.admin.banner.vo.ShopBannerUpdateReqVO;
import cn.csc.module.shop.convert.banner.ShopBannerConvert;
import cn.csc.module.shop.dal.dataobject.banner.ShopBannerDO;
import cn.csc.module.shop.service.banner.ShopBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigInteger;

import static cn.csc.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - banner管理")
@RestController
@RequestMapping("/shop/banner")
@Validated
public class ShopBannerController {

    @Resource
    private ShopBannerService shopBannerService;

    @PostMapping("/create")
    @ApiOperation("创建banner")
    @PreAuthorize("@ss.hasPermission('shop:banner:create')")
    public CommonResult<BigInteger> createBanner(@Valid @RequestBody ShopBannerCreateReqVO createReqVO) {
        return success(shopBannerService.createBanner(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新banner")
    @PreAuthorize("@ss.hasPermission('shop:banner:update')")
    public CommonResult<Boolean> updateBanner(@Valid @RequestBody ShopBannerUpdateReqVO updateReqVO) {
        shopBannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除banner")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:banner:delete')")
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") BigInteger id) {
        shopBannerService.deleteBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得banner")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:banner:query')")
    public CommonResult<ShopBannerRespVO> getBanner(@RequestParam("id") BigInteger id) {
        ShopBannerDO bannerDO = shopBannerService.getBanner(id);
        return success(ShopBannerConvert.INSTANCE.convert(bannerDO));
    }


    @GetMapping("/page")
    @ApiOperation("获得banner分页")
    @PreAuthorize("@ss.hasPermission('shop:banner:query')")
    public CommonResult<PageResult<ShopBannerRespVO>> getBannerPage(@Valid ShopBannerPageReqVO pageVO) {
        PageResult<ShopBannerDO> pageResult = shopBannerService.getBannerPage(pageVO);
        return success(ShopBannerConvert.INSTANCE.convertPage(pageResult));
    }

}
